package com.tinycloud.miracll.product.util;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;



@Component("basicAuthUtil")
public class BasicAuthUtil {

    public  ResponseEntity<String>  getHttpResultGet(String url, String userName, String passWord) {
        RestTemplateBuilder builder = new RestTemplateBuilder();
        RestTemplate restTemplate = builder.basicAuthentication(userName, passWord).build();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        return  responseEntity;
    }


    public ResponseEntity<String>  getHttpResultPost(String url, String msg, String userName, String passWord) {

        RestTemplateBuilder builder = new RestTemplateBuilder();
        RestTemplate restTemplate = builder.basicAuthentication(userName, passWord).build();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(msg, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
        return  responseEntity;

    }


}
